import java.util.Scanner;

public class LASTDIG {
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		int testcases = in.nextInt();
		while (testcases != 0) {
			int base = in.nextInt();
			int index = in.nextInt();
			System.out.println(calculateExponential(base, index));
			testcases--;
		}
	}

	private static int calculateExponential(int base, int index) {
		int result = 0;
		if (index == 0) {
			return 1;
		}
		if ((index % 4) == 0) {
			index = 4;
		} else {
			index = index % 4;
		}
		result = (((int) Math.pow(base, index)) % 10);
		return result;
	}
}
